﻿using System;
using System.Text;
using System.Web.UI.WebControls;
using FredCK.FCKeditorV2;
using Lswweb.Model;
using Lswweb.Domain;

namespace Lswweb.Controls
{
    public class SinglePageEdit : SkinnedWebControl
    {
        #region 成员字段和构造方法

        private TextBox txtTitle;
        private ImageUpload fileUpload;
        private ActionBar actionBar;
        private Image imgPreview;
        private FCKeditor fckEditor;
        private Button btnConfirm;

        private SinglePage pageType = (SinglePage)(-1);

        #endregion

        #region 初始化控件

        protected override void CreateChildControls()
        {
            base.CreateChildControls();
            if (!Page.IsPostBack)
            {
                if ((int)PageType == -1)
                    throw new SiteException(ExceptionType.PageNotFond);

                SinglePageInfo customPage = SinglePages.GetSinglePage(PageType);
                if (customPage != null)
                    PageInfoByForm = customPage;
            }
        }

        protected override void InitializeSkin(System.Web.UI.Control skin)
        {
            txtTitle = (TextBox)skin.FindControl("txtTitle");
            fileUpload = (ImageUpload)skin.FindControl("fileUpload");
            fckEditor = (FCKeditor)skin.FindControl("fckEditor");
            actionBar = (ActionBar)skin.FindControl("actionBar");
            imgPreview = (Image)skin.FindControl("imgPreview");
            btnConfirm = (Button)skin.FindControl("btnConfirm");

            fckEditor.Config["ImageBrowser"] = "true";
            fckEditor.Config["FlashBrowser"] = "true";
            btnConfirm.Text = this.Strings["Update"];

            btnConfirm.Click += new EventHandler(btnConfirm_Click);
        }

        void btnConfirm_Click(object sender, EventArgs e)
        {
            SinglePageInfo info = PageInfoByForm;
            if (fileUpload.HasFile)
            {
                string url = fileUpload.SaveAs();
                if (!string.IsNullOrEmpty(url))
                    info.ImageUrl = url;
                else
                    return;
            }

            SinglePages.Update(info);
        }

        #endregion

        #region 公共属性

        public SinglePage PageType
        {
            get { return ((int)pageType == -1) ? (SinglePage)SiteContext.Current.Id : pageType; }
            set { pageType = value; }
        }

        public SinglePageInfo PageInfoByForm
        {
            get 
            {
                SinglePageInfo customPageInfo = new SinglePageInfo();
                customPageInfo.Id=PageType;
                customPageInfo.Title=txtTitle.Text;
                customPageInfo.View=fckEditor.Value;
                return customPageInfo;
            }
            set 
            {
                if (value == null)
                    return;

                PageType=value.Id;
                txtTitle.Text=value.Title;
                fckEditor.Value=value.View;

                if (!string.IsNullOrEmpty(value.ImageUrl))
                {
                    imgPreview.ImageUrl = value.ImageUrl;
                    imgPreview.Visible = true;
                }
            }
        }

        #endregion

        protected override string DefaultSkinFilename
        {
            get { return "SinglePage/SingePageEdit.ascx"; }
        }
    }
}
